<template>
  <el-dialog v-model="visible" :title="!dataForm.filename ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="文件名称" prop="filename">
        <el-input v-model="dataForm.filename" placeholder="文件名称"></el-input>
      </el-form-item>
      <el-form-item label="航班联线ID" prop="flightlineid">
        <el-input v-model="dataForm.flightlineid" placeholder="航班联线ID"></el-input>
      </el-form-item>
      <el-form-item label="对应原始译后数据中的rownum,使用开始和结束行号来标识一个航班数据" prop="startrownum">
        <el-input v-model="dataForm.startrownum" placeholder="对应原始译后数据中的rownum,使用开始和结束行号来标识一个航班数据"></el-input>
      </el-form-item>
      <el-form-item label="对应原始译后数据中的rownum,结束行号" prop="endrownum">
        <el-input v-model="dataForm.endrownum" placeholder="对应原始译后数据中的rownum,结束行号"></el-input>
      </el-form-item>
      <el-form-item label="起飞时间" prop="takeofftime">
        <el-date-picker type="datetime" placeholder="起飞时间" v-model="dataForm.takeofftime" format="YYYY-MM-DD hh:mm:ss" value-format="YYYY-MM-DD hh:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item label="降落时间" prop="landingtime">
        <el-date-picker type="datetime" placeholder="降落时间" v-model="dataForm.landingtime" format="YYYY-MM-DD hh:mm:ss" value-format="YYYY-MM-DD hh:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item label="起飞机场经度" prop="deplong">
        <el-input v-model="dataForm.deplong" placeholder="起飞机场经度"></el-input>
      </el-form-item>
      <el-form-item label="起飞机场纬度" prop="deplat">
        <el-input v-model="dataForm.deplat" placeholder="起飞机场纬度"></el-input>
      </el-form-item>
      <el-form-item label="到达机场经度" prop="arrlong">
        <el-input v-model="dataForm.arrlong" placeholder="到达机场经度"></el-input>
      </el-form-item>
      <el-form-item label="到达机场纬度" prop="arrlat">
        <el-input v-model="dataForm.arrlat" placeholder="到达机场纬度"></el-input>
      </el-form-item>
      <el-form-item label="起飞机场4码" prop="dep4code">
        <el-input v-model="dataForm.dep4code" placeholder="起飞机场4码"></el-input>
      </el-form-item>
      <el-form-item label="降落机场4码" prop="arr4code">
        <el-input v-model="dataForm.arr4code" placeholder="降落机场4码"></el-input>
      </el-form-item>
      <el-form-item label="机号" prop="aircraftid">
        <el-input v-model="dataForm.aircraftid" placeholder="机号"></el-input>
      </el-form-item>
      <el-form-item label="是否航班（未用）" prop="isflight">
        <ren-select v-model="dataForm.isflight" dict-type="ISFLIGHT" placeholder="是否航班（未用）"></ren-select>
      </el-form-item>
      <el-form-item label="匹配状态" prop="matchstate">
        <el-input v-model="dataForm.matchstate" placeholder="匹配状态"></el-input>
      </el-form-item>
      <el-form-item label="分析状态" prop="analystate">
        <ren-select v-model="dataForm.analystate" dict-type="ANALYSTATE" placeholder="分析状态"></ren-select>
      </el-form-item>
      <el-form-item label="备注" prop="memo">
        <el-input v-model="dataForm.memo" placeholder="备注"></el-input>
      </el-form-item>
      <el-form-item label="航班号" prop="flightno">
        <el-input v-model="dataForm.flightno" placeholder="航班号"></el-input>
      </el-form-item>
      <el-form-item label="航班ID" prop="flightid">
        <el-input v-model="dataForm.flightid" placeholder="航班ID"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="timestamp">
        <el-date-picker type="datetime" placeholder="创建时间" v-model="dataForm.timestamp" format="YYYY-MM-DD hh:mm:ss" value-format="YYYY-MM-DD hh:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item label="有效标识" prop="isvalid">
        <el-input v-model="dataForm.isvalid" placeholder="有效标识"></el-input>
      </el-form-item>
      <el-form-item label="更新时间" prop="modifytime">
        <el-date-picker type="datetime" placeholder="更新时间" v-model="dataForm.modifytime" format="YYYY-MM-DD hh:mm:ss" value-format="YYYY-MM-DD hh:mm:ss"></el-date-picker>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  filename: "",
  flightlineid: "",
  startrownum: "",
  endrownum: "",
  takeofftime: "",
  landingtime: "",
  deplong: "",
  deplat: "",
  arrlong: "",
  arrlat: "",
  dep4code: "",
  arr4code: "",
  aircraftid: "",
  isflight: "",
  matchstate: "",
  analystate: "",
  memo: "",
  flightno: "",
  flightid: "",
  timestamp: "",
  isvalid: "",
  modifytime: "",
});

const rules = ref({
  filename: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  flightlineid: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  startrownum: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  endrownum: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  takeofftime: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  landingtime: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  deplong: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  deplat: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  arrlong: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  arrlat: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  dep4code: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  arr4code: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  aircraftid: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  isflight: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  matchstate: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  analystate: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  memo: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  flightno: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  flightid: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  timestamp: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  isvalid: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  modifytime: [{ required: true, message: t("validate.required"), trigger: "blur" }],
});

const init = (filename?: String) => {
  visible.value = true;
  dataForm.filename = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (filename) {
    getInfo(filename);
  }
};

// 获取信息
const getInfo = (filename: String) => {
  baseService.get("/sys/flightlinesinfo/" + filename).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.filename ? baseService.post : baseService.put)("/sys/flightlinesinfo", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>